/*
Hermes Core: General Purpose Mobile Agent Platform
Copyright (C) 2006 UNICAM - Università degli Studi di Camerino

Group leaders: Flavio Corradini and Emanuela Merelli

Developers: Ezio Bartocci, Diego Bonura, Rosario Culmone,
Davide Lorenzoli, Leonardo Mariani, Francesca Piersigilli, 
Lorenzo Scortichini, Leonardo Vito.

GNU Lesser Public License

This library is free software; you can redistribute it 
and/or modify it under the terms of the GNU Lesser General 
Public License as published by the Free Software Foundation, 
version 2.1 of the License.

This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Emanuela Merelli
Dipartimento di Matematica ed Informatica
Università di Camerino
via Madonna delle Carceri, 62032 Camerino, Italy
E.Mail: emanuela.merelli@unicam.it
*/
package hermesV2.agent;

import hermesV2.PlaceAddress;

import java.io.Serializable;

import java.util.Date;


/**
 * It is the Identificator class that every agent must own.
 *
 * @author Diego Bonura
 * @author Davide Lorenzoli
 */
public class Identificator implements Serializable, Cloneable {
    /**
	 * 
	 */
	private static final long serialVersionUID = -8129378545596646999L;

	/** <CODE>Static</CODE> is a static counter. */
    private static int serial;

    /** It is the place addres where the agent is born. */
    private PlaceAddress bornPlaceAddress;

    /** <CODE>SerialID</CODE> is the serial number of the instance. */
    private int serialId;

    /** TRUE when the agent is a service agent false otherwise. */
    private boolean isServiceAgent;

    /** Time when the data has been created. */
    private Date creationDate;

    /** The name of the agent */
    private String name;

    /**
     * Constructor.
     *
     * @param placeAddress The place addres where the agent is born.
     * @param name The name of the agent.
     * @param isServiceAgent TRUE if the agent is a <CODE>ServiceAgent</CODE>.
     */
    public Identificator(PlaceAddress placeAddress, String name, boolean isServiceAgent) {
        this.bornPlaceAddress = placeAddress;
        this.isServiceAgent = isServiceAgent;
        this.name = name;
        this.serialId = serial++;
        this.creationDate = new Date();
    }    

    /**
     * It returns a serialId.
     *
     * @return An int.
     */
    public int getSerialId() {
        return this.serialId;
    }

    /**
     * It returns the name of the agent.
     *
     * @return The name of the agent.
     */
    public String getName() {
        return this.name;
    }

    /**
     * It returns the place addres where the agent is born.
     *
     * @return The place address.
     */
    public PlaceAddress getBornPlaceAddress() {
        return this.bornPlaceAddress;
    }

    /**
     * Return TRUE if the Agent is a service agent, FALSE otherwise
     *
     * @return A boolean.
     */
    public boolean isServiceAgent() {
        return this.isServiceAgent;
    }

    /**
     * Returns the Time when the agent was created
     *
     * @return <CODE>Date</CODE>
     */
    public Date getCreationDate() {
        return this.creationDate;
    }

    /**
     * It returns true if the id specified below is equals to the current one.
     *
     * @param ident An <CODE>Identificator</CODE>.
     *
     * @return A boolean.
     */
    public final boolean equals(Object ident) {
        boolean success;
    	if (this == ident) {
            success= true;
        }else{
        	success = this.toString().equals(ident.toString());
        }
        return success;
    }

    /**
     * It returns the hashcode of the Object
     *
     * @return a int.
     */
    public final int hashCode() {
        return (this.bornPlaceAddress.getPort() + this.serialId + Integer.parseInt((this.bornPlaceAddress.getIp().replace('.', '0')).substring(this.bornPlaceAddress.getIp().length() - 7)) + (int) this.creationDate.getTime());
    }

    /**
     * It returns a <CODE>String</CODE>.
     *
     * @return A <CODE>String</CODE>.
     */
    public String toString() {
        return "|AgentName: " + name + "|bornPlaceAddress: " + bornPlaceAddress + "|isServiceAgent: " + isServiceAgent + "|serial: " + serialId + "|creationDate: " + creationDate.toString();
    }
}
